﻿var menu_vm = avalon.define({
    $id: 'menu',
    menus: JSON.parse($('#menu_data').val()),
    new_custom_menu: {},
    be_edit_menu: null,
    fetch_data: function () {
        $.get('/site/getmenus').success(this.on_fetch_menu_success);
    },
    on_fetch_menu_success: function (data) {
        menu_vm.menus = data;
    },
    create_menu: function () {
        $.post('/site/newcustommenu', menu_vm.new_custom_menu).success(menu_vm.on_create_menu_success);
    },
    on_create_menu_success: function () {
        menu_vm.new_custom_menu = { Name: null, Link: null };
        menu_vm.fetch_data();
    },
    save_menu: function () {
        $.post('/site/savemenu', menu_vm.be_edit_menu.$model).success(menu_vm.fetch_data);
    },
    remove_menu: function () {
        $.post('/site/removemenu', menu_vm.be_edit_menu.$model).success(menu_vm.on_remove_menu_success);
    },
    on_remove_menu_success: function () {
        menu_vm.be_edit_menu = null;
        menu_vm.fetch_data();
    },
    edit_menu: function (menu) {
        menu_vm.be_edit_menu = menu;
    }
});